<template>
	<view>
		<u-navbar :border-bottom="false" title="登录" back-icon-color="#000" title-color="#000" :background="{}"></u-navbar>
		
		<!-- 登录弹窗 -->
		<view class="loginPopup">
			<view class="loginBox">
				<view class="title">快速登录</view>
				<image class="logo" src="/static/icon2/login-tips.png"></image>
				<view class="description">请点击授权按钮，快速登录</view>
			</view>
			<!-- <button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo">授权登录</button> -->
			<view class="btn">
				<u-button type="success" @click="getUserInfo">授权登录</u-button>
			</view>
			
			<view class="u-p-t-30 u-flex u-row-center u-p-l-20">
				<u-checkbox active-color="#dd592f" label-size="26" shape="circle" v-model="checked">已阅读<text style="color: #1f87f9;" 
					@click.stop="jump('/pages/news/article?id=19')">《用户隐私协议》</text></u-checkbox>
			</view>
		</view>
	</view>
</template>
<script>
let clear;
export default {
	data() {
		return {
			userInfo:{},
			checked:!0
		};
	},
	methods: {
		getUserInfo(){
			if(!this.checked){
				this.$u.toast('请先阅读并同意相关协议')
				return
			}
			wx.getUserProfile({
				desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: (res) => {
					this.login(res)
				}
			})
		},
		login(res){
			if(!this.userInfo.code){
				this.$u.toast('登录错误，请重试')
				this.getOpenid()
				return
			}
			if("getUserProfile:ok" == res.errMsg){
				this.userInfo.iv = res.iv
				this.userInfo.encryptedData = res.encryptedData
				this.userInfo.recommend = this.vuex_recommend || '' //推广码
				// process.env.NODE_ENV == 'development' ? this.$u.api.loginTest({
				// 	mobile:'15505512229',
				// 	password:'512229'
				// }).then(res=>{
				// 	this.$u.vuex('token',res)
				// 	let pages = getCurrentPages()
				// 	if (pages.length >= 2) {
				// 		let beforePage = pages[pages.length - 2]
				// 		uni.navigateBack({
				// 			success:()=>{
				// 				beforePage.onLoad(beforePage.options)
				// 			}
				// 		})
				// 	} else {
				// 		uni.reLaunch({
				// 			url:"/pages/index/index"
				// 		})
				// 	}
				// }) : 
				this.$u.api.appLogin({...this.userInfo}).then(res=>{
					this.$u.vuex('token',res.token)
					let pages = getCurrentPages()
					if (pages.length >= 2) {
						let beforePage = pages[pages.length - 2]
						uni.navigateBack({
							success:()=>{
								beforePage.onLoad(beforePage.options)
							}
						})
					} else {
						uni.reLaunch({
							url:"/pages/index/index"
						})
					}
					
					this.$store.dispatch('userInfo',this)
				}).catch(err=>{
					this.getOpenid()
				})
			}
		},
		getOpenid(){
			uni.login({
				provider:'weixin',
				success: ({code}) => {
					this.userInfo = {code}
				}
			})
		}
	},
	onLoad() {
		this.getOpenid()
	}
}
</script>

<style lang="scss">
page{background: url(#{$imgUrl}/bg.png) repeat;}
.loginPopup {
	position: fixed;
	top: 45%;
	left: 50%;
	transform: translateX(-50%) translateY(-50%);
	width: 600rpx;
	background-color: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	padding-bottom: 40rpx;

	.loginBox {
		display: flex;
		flex-direction: column;
		align-items: center;

		.logo {
			width: 240rpx;
			height: 240rpx;
			padding: 46rpx;
		}

		.title {
			font-size: 36rpx;
			color: #333;
			font-weight: bold;
			line-height: 100rpx;
		}

		.description {
			margin: 10rpx 0 30rpx;
			font-size: 30rpx;
			color: #333;
		}
	}
	.btn{padding: 0 30rpx;}
	button{
		width: 80%;line-height: 60rpx;font-size: 32rpx;
	}
	button::after {border: 0;}
}
</style>
